package anotador;

import gate.*;
import gate.creole.*;
import gate.jape.Batch;
import gate.jape.JapeException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mauricio
 */
public class Controller {
    
    private SerialAnalyserController controller;
    private Batch batch;
    private GateCorpus corpus;

    public Controller() {
        try {
            controller = (SerialAnalyserController) Factory.createResource( "gate.creole.SerialAnalyserController",
                    Factory.newFeatureMap(), Factory.newFeatureMap(), "ANNIE_" + Gate.genSym());
        } catch (ResourceInstantiationException ex) {
            ex.printStackTrace();
        }
    }

    public void setCorpus(GateCorpus corpus) {
        this.corpus = corpus;
        controller.setCorpus(corpus.getCorpus());
    }

    public void addDefaults()
    {
        ProcessingResource pr;
        for( int i = 0; i < ANNIEConstants.PR_NAMES.length; i++ )
        {
            FeatureMap params = Factory.newFeatureMap();
            
            try {
                if (!ANNIEConstants.PR_NAMES[i].equals("gate.creole.gazetteer.DefaultGazetteer"))
                {
                    System.out.println( ANNIEConstants.PR_NAMES[i] );
                    pr = (ProcessingResource) Factory.createResource( ANNIEConstants.PR_NAMES[i], params );
                    controller.add( pr );
                }
            } catch (ResourceInstantiationException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void addGazetteer(URL list)
    {
        FeatureMap params = Factory.newFeatureMap();
        params.put("listsURL", list);
        params.put("caseSensitive", false);
        
        ProcessingResource pr;
        try {
            pr = (ProcessingResource) Factory.createResource( "gate.creole.gazetteer.DefaultGazetteer", params );
            controller.add(pr);
        } catch (ResourceInstantiationException ex) {
            ex.printStackTrace();
        }
    }

    public void addTransducer(URL japeGrammar)
    {
        try {
            batch = new Batch(japeGrammar, "UTF-8");
        } catch (JapeException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void execute()
    {
        try {
            //ejecutar controlador
            controller.execute();
            batch.transduce(corpus.getCorpus());
        } catch (JapeException ex) {
            ex.printStackTrace();
        } catch (ExecutionException ex) {
            ex.printStackTrace();
        }
    }
}